package com.xiaoxiang.cameracontrol;

import android.app.Activity;
import android.os.Handler;

import com.xiaoxiang.cameracontrol.zsocket.Message;
import com.xiaoxiang.cameracontrol.zsocket.SocketClient;

import java.util.HashMap;

public class CameraSocket extends SocketClient {
    private Handler login_handler;
    private Handler camera_handler;
    public String my_id;
    public String now_person="";
    public String now_type="";
    public String server_host;
    public String local_host;

    private static final int DEVICE_ID = 0;
    private static final int DENY_DEVICE = 1;
    public static final int START_RECORD = 2;
    public static final int STOP_RECORD = 3;
    public static final int NEW_SEQUEENCE = 4;
    public static final int UPDATE_STATE = 5;


    public CameraSocket(Handler login_handler){
        this.login_handler = login_handler;
    }

    @Override
    protected void connected() {
        this.server_host = this.host+":"+String.valueOf(this.port);
        this.local_host = this.socket.getLocalAddress().getHostAddress()+":"+
                String.valueOf(this.socket.getLocalPort());
    }

    @Override
    protected void disconnected() {
        this.server_host = "";
        this.local_host = "";
    }

    @Override
    protected void recv_message(byte[] message) {
        HashMap<String, byte[]> info = (HashMap<String, byte[]>) Message.decode(message);
        int code = (int)Message.decode(info.get("code"));
        switch(code){
            case DEVICE_ID:
                my_id = (String)Message.decode(info.get("device_id"));
                android.os.Message msg = login_handler.obtainMessage();
                msg.what = DEVICE_ID;
                login_handler.sendMessage(msg);
                break;
            case DENY_DEVICE:
                android.os.Message msg1 = login_handler.obtainMessage();
                msg1.what = DENY_DEVICE;
                login_handler.sendMessage(msg1);
                break;
            case START_RECORD:
                android.os.Message msg2 = camera_handler.obtainMessage();
                msg2.what = START_RECORD;
                camera_handler.sendMessage(msg2);
                break;
            case STOP_RECORD:
                android.os.Message msg3 = camera_handler.obtainMessage();
                msg3.what = STOP_RECORD;
                camera_handler.sendMessage(msg3);
                break;
            case NEW_SEQUEENCE:
                this.now_person = (String)Message.decode(info.get("person"));
                this.now_type = (String)Message.decode(info.get("type"));
                if(this.now_person==null || this.now_type==null){
                    break;
                }
                android.os.Message msg4 = camera_handler.obtainMessage();
                msg4.what = NEW_SEQUEENCE;
                msg4.obj = this.now_person+"&"+this.now_type;
                camera_handler.sendMessage(msg4);
                break;
            default:
                this.logger.info("未知的消息类型："+String.valueOf(code));
        }
    }

    public void send_auth(String my_id){
        HashMap<String, byte[]> message = getMessageHead(DEVICE_ID);
        message.put("device_id", Message.encode(my_id));
        this.send_bytes(Message.encode(message));
    }

    public HashMap<String, byte[]> getMessageHead(int messageType){
        HashMap<String, byte[]> result = new HashMap();
        result.put("code", Message.encode(messageType));
        return result;
    }

    public void setCameraHandler(Handler handler){
        this.camera_handler = handler;
    }

    public void send_state(int is_record, int battery, int storage){
        HashMap<String, byte[]> message = getMessageHead(UPDATE_STATE);
        message.put("device_id", Message.encode(this.my_id));
        message.put("is_record", Message.encode(is_record));
        message.put("battery", Message.encode(battery));
        message.put("storage", Message.encode(storage));
        message.put("person_id", Message.encode(this.now_person));
        message.put("person_seq", Message.encode(this.now_type));
        this.send_bytes(Message.encode(message));
    }
}
